﻿using Minecraft.CacheRedis;
using Minecraft.Config;
using Minecraft.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.BLL.mysql
{
	public class DropinfoBLL : BaseBLL
	{
		private static RedisCacheHelper redisCacheHelper = new RedisCacheHelper();
		private static RedisKey redisKeyEnum = RedisKey.Dropinfo;
		public static List<DropinfoModel> GetList(int playerId, out bool fromCache)
		{
			fromCache = false;
			string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString());
			var cacheList = redisCacheHelper.StringGet<List<DropinfoModel>>(redisKey);
			if (cacheList != null)
			{
				fromCache = true;
				return cacheList;
			}
			else
			{
				var model = new DropinfoModel();
				var list = GetList(model, new KeyValuePair<string, int>(nameof(model.PlayerId), playerId));
				redisCacheHelper.StringSet(redisKey, list, CommonConfig.DefRedisExpiry);
				return list;
			}
		}
		/// <summary>
		/// 批量更新玩家的家具信息（对于数据库操作时，先删除后插入）
		/// </summary>
		/// <param name="isCacheAfterDbOpt">缓存后是否进行数据库操作</param>
		public static bool BatchUpdate(List<DropinfoModel> list, int playerId, bool isCacheAfterDbOpt)
		{
			string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString());
			redisCacheHelper.StringSet(redisKey, list, CommonConfig.DefRedisExpiry);
			if (isCacheAfterDbOpt)
			{
				var model = new DropinfoModel();
				return BatchUpdate(list, new KeyValuePair<string, int>(nameof(model.PlayerId), playerId));
			}
			return true;
		}
	}
}
